package com.yzy.fly.mapper;

import com.yzy.fly.entity.role.FlyTableColumn;
import com.yzy.fly.kernel.mapper.FlyBasicMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Collection;
import java.util.List;

@Mapper
public interface FlyTableColumnMapper extends FlyBasicMapper<FlyTableColumn> {

    @Select("select * from fly_table_column a where 1=1 ")
    List<FlyTableColumn> selectAllList();

    @Select("select * from fly_table_column a where 1=1 and a.systemId = #{systemId}")
    List<FlyTableColumn> selectAllBySystemId(@Param("systemId") Integer systemId);

    @Select("select * from fly_table_column a where 1=1 and a.tableId = #{tableId} order by seq asc")
    List<FlyTableColumn> selectAllByTableId(@Param("tableId") Integer tableId);

    @Select("select * from fly_table_column a where 1=1 and a.parentId = #{parentId} and a.systemId = #{systemId}")
    List<FlyTableColumn> selectAllByParentIdAndSystemId(@Param("parentId") Integer parentId, @Param("systemId") Integer systemId);

    @Select("select * from fly_table_column a where 1=1 and (a.id = #{parentId} or a.parentId = #{parentId}) and a.systemId = #{systemId}")
    List<FlyTableColumn> selectAllByParentIdAndSystemId_ContainsId(@Param("parentId") Integer parentId, @Param("systemId") Integer systemId);

    @Select("select * from fly_table_column a where 1=1 and a.defModelId = #{defModelId} and a.systemId = #{systemId}")
    List<FlyTableColumn> selectAllByDefIdIdAndSystemId(@Param("defModelId") Integer defModelId, @Param("systemId") Integer systemId);

    /**
     * 批量插入（mysql）
     * @param entityList
     * @return
     */
    Integer insertBatchSomeColumn(Collection<FlyTableColumn> entityList);

    @Update("update fly_table_column a set seq = seq - 1  where id = #{id} and seq = #{seq}")
    int upById(@Param("id") int id, @Param("seq")int seq);

    @Update("update fly_table_column a set seq = seq + 1  where id = #{id} and seq = #{seq}")
    int downById(@Param("id")int id, @Param("seq")int seq);

    @Update("update fly_table_column a set seq = seq - 1  where tableId = #{tableId} and seq = #{seq}")
    int upByTableId(@Param("tableId") int tableId, @Param("seq")int seq);

    @Update("update fly_table_column a set seq = seq + 1  where tableId = #{tableId} and seq = #{seq}")
    int downByTableId(@Param("tableId")int tableId, @Param("seq")int seq);

    @Select("select max(a.seq) from fly_table_column a where tableId = #{tableId}")
    Integer maxSeqNo(@Param("tableId")int tableId);
}
